include(${PROJECT_SOURCE_DIR}/cmake/openssl.cmake)

file(GLOB src ${PROJECT_SOURCE_DIR}/src/sqlcipher/*.c)
list(REMOVE_ITEM src ${PROJECT_SOURCE_DIR}/src/sqlcipher/geopoly.c)
list(REMOVE_ITEM src ${PROJECT_SOURCE_DIR}/src/sqlcipher/tclsqlite.c)
list(REMOVE_ITEM src ${PROJECT_SOURCE_DIR}/src/sqlcipher/shell.c)

add_executable(sqlcipher ${src} ${PROJECT_SOURCE_DIR}/src/sqlcipher/shell.c)
target_compile_definitions(sqlcipher PRIVATE -DSQLITE_HAS_CODEC -DSQLCIPHER_CRYPTO_OPENSSL -D_HAVE_SQLITE_CONFIG_H -DBUILD_sqlite -DNDEBUG -DSQLITE_THREADSAFE=1 -DSQLITE_ENABLE_MATH_FUNCTIONS -DSQLITE_TEMP_STORE=2 -DSQLITE_CORE)
target_include_directories(sqlcipher PRIVATE ${PROJECT_SOURCE_DIR}/include/sqlcipher)
target_include_directories(sqlcipher PRIVATE ${PROJECT_BINARY_DIR}/openssl_output/include)
target_link_directories(sqlcipher PRIVATE ${PROJECT_BINARY_DIR}/openssl_output/lib64)
target_link_libraries(sqlcipher PRIVATE crypto ssl)

add_library(sqlcipher_lib ${src})
target_compile_definitions(sqlcipher_lib PRIVATE -DSQLITE_HAS_CODEC -DSQLCIPHER_CRYPTO_OPENSSL  -D_HAVE_SQLITE_CONFIG_H -DBUILD_sqlite -DNDEBUG -DSQLITE_THREADSAFE=1 -DSQLITE_ENABLE_MATH_FUNCTIONS -DSQLITE_TEMP_STORE=2 -DSQLITE_CORE)
target_include_directories(sqlcipher_lib PRIVATE ${PROJECT_SOURCE_DIR}/include/sqlcipher)
target_include_directories(sqlcipher_lib PRIVATE ${PROJECT_BINARY_DIR}/openssl_output/include)
target_link_directories(sqlcipher_lib PRIVATE ${PROJECT_BINARY_DIR}/openssl_output/lib64)
target_link_libraries(sqlcipher_lib PRIVATE crypto ssl)

if (TARGET_PLATFORM STREQUAL "win")
    target_compile_definitions(sqlcipher PRIVATE -DSQLITE_OS_WIN=1)
    target_link_libraries(sqlcipher PRIVATE crypt32 ws2_32 gdi32)
    #target_compile_options(sqlcipher_lib PRIVATE -static-libstdc++ -static-libgcc -Wl,--enable-auto-image-base
    #       -Wl,--add-stdcall-alias -Wl,--enable-auto-import)

    target_compile_definitions(sqlcipher_lib PRIVATE -DSQLITE_OS_WIN=1 )
    target_link_libraries(sqlcipher_lib PRIVATE crypt32 ws2_32 gdi32)
    #target_compile_options(sqlcipher_lib PRIVATE -static-libstdc++ -static-libgcc -Wl,--enable-auto-image-base
    #        -Wl,--add-stdcall-alias -Wl,--enable-auto-import)
elseif (TARGET_PLATFORM STREQUAL "linux")
    target_compile_definitions(sqlcipher PRIVATE -DSQLITE_OS_UNIX=1)
    target_link_libraries(sqlcipher PRIVATE m dl pthread)

    target_compile_definitions(sqlcipher_lib PRIVATE -DSQLITE_OS_UNIX=1)
    target_link_libraries(sqlcipher_lib PRIVATE m dl pthread)
endif ()

add_dependencies(sqlcipher openssl-install)
add_dependencies(sqlcipher_lib openssl-install)


#include(ExternalProject)
#include(${PROJECT_SOURCE_DIR}/cmake/openssl.cmake)
#
#set(SQLCIPHER_SOURCE_DIR ${PROJECT_BINARY_DIR}/third_party/sqlcipher)
#
#if (TARGET_PLATFORM STREQUAL "win")
#    set(host x86_64-w64-mingw32)
#    set(LIBS -lcrypto\ -lcrypt32\ -lws2_32\ -lssl\ -lgdi32)
#elseif (TARGET_PLATFORM STREQUAL "linux")
#    set(host x86_64-linux-gnu)
#    set(LIBS -lcrypto\ -lssl)
#endif ()
#
#set(CFLAGS -DSQLITE_HAS_CODEC\ -DSQLCIPHER_CRYPTO_OPENSSL)
#set(CPPFLAGS -I${PROJECT_BINARY_DIR}/openssl_output/include)
#set(LDFLAGS -L${PROJECT_BINARY_DIR}/openssl_output/lib64)
#
#ExternalProject_Add(sqlcipher
#        URL https://gitee.com/madision/sqlcipher/repository/archive/master.zip
#        URL_MD5 ce69488a7fde3aa7c8d5ac0f375b418e
#        DOWNLOAD_NAME sqlcipher_3_46_1.zip
#        SOURCE_DIR ${SQLCIPHER_SOURCE_DIR}
#        CONFIGURE_COMMAND cd ${SQLCIPHER_SOURCE_DIR} && ./configure --prefix=${PROJECT_BINARY_DIR}/sqlcipher_output --host=${host}
#        --enable-tempstore=yes --enable-shared=no --with-crypto-lib=none CFLAGS=${CFLAGS} CPPFLAGS=${CPPFLAGS} LIBS=${LIBS} LDFLAGS=${LDFLAGS}
#        BUILD_COMMAND cd ${SQLCIPHER_SOURCE_DIR} && make -j4
#        INSTALL_COMMAND cd ${SQLCIPHER_SOURCE_DIR} && make install
#)
#
#if (TARGET_PLATFORM STREQUAL "win")
#    ExternalProject_Add_Step(
#            sqlcipher
#            shell-build
#            COMMAND cd ${SQLCIPHER_SOURCE_DIR} && cp ${PROJECT_SOURCE_DIR}/tool/shell.sh ./ && sh ./shell.sh
#            DEPENDEES configure
#            DEPENDERS build
#            ALWAYS 1
#            COMMENT ""
#    )
#endif ()
#
#ExternalProject_Add_StepTargets(
#        sqlcipher install
#)





